package bdqn.t09.controller;

import bdqn.t09.pojo.*;
import bdqn.t09.service.*;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CustomController2 {

    // 客户/公司
    @Resource
    private AsCustomsService customsService;

    // 省
    @Resource
    private HatProvinceService provinceService;

    // 城
    @Resource
    private HatCityService cityService;

    // 区
    @Resource
    private HatAreaService areaService;

    // 配置（词典）信息
    @Resource
    private AsSystemconfigService systemconfigService;

    //访问修改客户信息页面
    @RequestMapping(value ={"update"},method = RequestMethod.GET)
    public String toUpdateCustoms(int id, Model model){
        model.addAttribute("id",id);
        return "updateCustoms";
    }

    //加载客户信息
    @GetMapping("/load")
    @ResponseBody
    public Object getById(Long id){
        Map map=new HashMap();
        //拿到客户对象
        AsCustoms customs =  customsService.selectByPrimaryKey(id);
        map.put("customs",customs);
        // 拿到省
        List<HatProvince> hatProvinces = provinceService.selectAllPrimary();
        map.put("hatProvincesList",hatProvinces);
        // 拿到市
        List<HatCity> hatCity = cityService.selectAllCity();
        map.put("hatCityList",hatCity);
        // 拿到区
        List<HatArea> hatAreas = areaService.selectAllArea();
        map.put("hatAreasList",hatAreas);
        // 拿到企业类型
        List<AsSystemconfig> customtype = systemconfigService.findConfigById(5);
        map.put("customtypeList",customtype);
        // 拿到证件类型
        List<AsSystemconfig> cardtype = systemconfigService.findConfigById(6);
        map.put("cardtypeList",cardtype);

        return map;
    }

    //修改客户信息
    @PutMapping("/updateCustoms")
    @ResponseBody
    public Object update(@RequestBody AsCustoms customs) {
        int count=customsService.updateByPrimaryKey(customs);
        return Integer.valueOf(count);
    }
}
